'use strict';


const db = uniCloud.database();
const dbCmd = db.command // 取指令

async function searchHotel(event){
	function HotelBean() {
		var id
	  var image;
	  var name;
	  var score;
	  var service;
	  var address;
	  var distance;
	  var price;
	  var test;
	}
	function pushArr(arr){
		let result = []
	 	arr.map((item)=>{
			var hotelBean = new HotelBean();
			hotelBean.id = item._id
			hotelBean.image = item.hotel_image;
			hotelBean.name = item.hotelName;
			hotelBean.score = item.score;
			let service = item.serviceNameList.join("/")
			hotelBean.service = service;
			hotelBean.address = item.hotel_address;
			// hotelBean.distance = '3.5';
			hotelBean.price = item.price;
			result.push(hotelBean);
		})
		return result
	}
	let res = []
	const {location,number,district,type} = event
	const num = number == '' ? 1 : number
	// 搜索
	if(type == 2){
		await db.collection('xinxuan_hotel').where({
			is_on_open: true,
			hotelName : new RegExp(`${event.hotelName}`)
		}).get().then((result)=>{
			res = pushArr(result.data)
			console.log(222,res)
		});
		return {
			code: 0,
			msg:'',
			data: res
		}
	}
	// 价格排序
	if(type == 1){
		await db.collection('xinxuan_hotel').where({
			is_on_open: true,
			city:location,
			number: dbCmd.gte(num)
		}).orderBy('price','desc').get().then((result)=>{
			res = pushArr(result.data)
			console.log(111,res)
		});
		return {
			code: 0,
			msg:'',
			data: res
		}
	}
	if(district == ''){
		await db.collection('xinxuan_hotel').where({
			is_on_open: true,
			city:location,
			number: dbCmd.gte(num)
		}).get().then((result)=>{
			res = pushArr(result.data)
			console.log(333,res)
		});
	}else{
		await db.collection('xinxuan_hotel').where({
			is_on_open: true,
			city:location,
			number: dbCmd.gte(num),
			district
		}).get().then((result)=>{
			res = pushArr(result.data)
		});
	}
	
	return {
		code: 0,
		msg:'',
		data: res
	}
}

exports.main = searchHotel;